package com.sixth.service.impl;

import com.sixth.bean.MarketAddress;
import com.sixth.bean.MarketAddressExample;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketAddressMapper;
import com.sixth.service.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName WxAddressServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/11 15:04
 **/
@Transactional
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;


    @Override
    public BasePageInfoVo<MarketAddress> list(Integer userId) {


        MarketAddressExample example = new MarketAddressExample();

        // 按照新建时间降序排序，默认地址至于首行
        example.setOrderByClause("is_default desc, update_time desc");

        MarketAddressExample.Criteria criteria = example.createCriteria();

        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);

        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);

        long total = marketAddresses.size();
        BasePageInfoVo<MarketAddress> pageInfoVo = new BasePageInfoVo<>(total,
                1, marketAddresses.size(), 1, marketAddresses);
        return pageInfoVo;
    }

    @Override
    public MarketAddress detail(Integer userId, Integer id) {

        MarketAddressExample example = new MarketAddressExample();

        MarketAddressExample.Criteria criteria = example.createCriteria();

        criteria.andUserIdEqualTo(userId).andIdEqualTo(id).andDeletedEqualTo(false);

        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);

        if (marketAddresses.size() == 1) {
            return marketAddresses.get(0);

        }
        return null;
    }


    @Override
    public int save(MarketAddress marketAddress, Integer userId) {

        // 默认地址应该只有一个

        // 更新时间
        Date date = new Date();

        marketAddress.setUpdateTime(date);
        // TODO 暂时设置地区编码和邮政编码一致
        marketAddress.setPostalCode(marketAddress.getAreaCode());

        // 如果新增或修改地址为默认地址
        // 则修改为为默认地址，需要将之前的默认地址设置为非默认
        if (marketAddress.getIsDefault()) {
           marketAddressMapper.updateIsDefault();
        }


        int affectRows = 0;

        // 通过传入marketAddress进行判断，如果id=0，ze是新增地址
        if (marketAddress.getId() == 0) {
            // 设置新建时间
            marketAddress.setAddTime(date);

            // 设置用户Id
            marketAddress.setUserId(userId);

            // 新增地址
            affectRows = marketAddressMapper.insertSelective(marketAddress);

        } else {
            // 根据id更新
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(marketAddress.getId());

            affectRows = marketAddressMapper.updateByExampleSelective(marketAddress, example);
        }
        return affectRows > 0 ? Constant.OK : Constant.NON;

    }

    @Override
    public int delete(Integer id, Integer userId) {

        MarketAddressExample example = new MarketAddressExample();

        MarketAddressExample.Criteria criteria = example.createCriteria();

        // 设置要删除的数据的条件
        criteria.andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);

        MarketAddress marketAddress = new MarketAddress();

        // 逻辑删除
        marketAddress.setDeleted(true);

        int affectRows = marketAddressMapper.updateByExampleSelective(marketAddress, example);

        return affectRows > 0 ? Constant.OK : Constant.NON;

    }
}
